/*
* ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
* Without permission, no one has the right to obtain, disclose or use this software in any way.
*/

package ecarx.xsf.notificationcenter.push;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.ecarx.eas.framework.sdk.push.PushAPI;
import com.ecarx.eas.sdk.ECarXApiClient;
import com.ecarx.sdk.push.api.IPushListener;



public class PushV3 implements NotificationPush {

    private static final String TAG = "PushV3";
    private static volatile PushV3 sInstance;
    private PushAPI mPushAPI;

    private PushV3() {
        try {
            mPushAPI = PushAPI.getInstance();
        } catch (NoClassDefFoundError error) {
            error.printStackTrace();
            Log.e(TAG, "Init push api error: " + error.getMessage());
        }
    }

    public static PushV3 getInstance() {
        if (sInstance == null) {
            synchronized (PushV3.class) {
                if (sInstance == null) {
                    sInstance = new PushV3();
                }
            }
        }
        return sInstance;
    }

    private PushAPI getPushAPI() {
        try {
            mPushAPI = PushAPI.getInstance();
        } catch (NoClassDefFoundError error) {
            error.printStackTrace();
        }
        return mPushAPI;
    }

    @Override
    public void init(Context context) {
        Log.i(TAG, "PushV3 pushSDK init called");
        if (getPushAPI() == null) {
            return;
        }
        getPushAPI().init(context.getApplicationContext(), new ECarXApiClient.Callback() {
            @Override
            public void onAPIReady(boolean ready) {
                Log.i(TAG, "PushManager doOnAPIReady");
                setLogEnable(true);
            }
        });
    }

    /**
     * 解除推送服务
     */
    public void release() {
        if (getPushAPI() == null) {
            return;
        }
        getPushAPI().release();
    }

    /**
     * 上行消息
     *
     * @param payload  消息体，必须为JsonString格式
     * @param listener 回调
     * @return 执行结果
     */
    public boolean publish(String payload, PushListener listener) {
        if (getPushAPI() == null) {
            return false;
        }
        return getPushAPI().publish(payload, listener);
    }

    /**
     * 接受推送消息(暂未实现)
     *
     * @param listener 回调
     * @return 执行结果
     */
    public boolean subscribe(PushListener listener) {
        if (getPushAPI() == null) {
            return false;
        }
        return getPushAPI().subscribe(listener);
    }

    /**
     * 拒收推送消息(暂未实现)
     *
     * @param listener 回调
     * @return 执行结果
     */
    public boolean unsubscribe(PushListener listener) {
        if (getPushAPI() == null) {
            return false;
        }
        return getPushAPI().unsubscribe(listener);
    }

    /**
     * 查询别名
     *
     * @param listener 回调
     * @return 执行结果
     */
    public boolean queryAlias(PushListener listener) {
        if (getPushAPI() == null) {
            return false;
        }
        return getPushAPI().queryAlias(listener);
    }

    /**
     * 绑定别名
     *
     * @param alias    别名
     * @param listener 回调
     * @return 执行结果
     */
    public boolean bindAlias(String alias, PushListener listener) {
        if (getPushAPI() == null) {
            return false;
        }
        return getPushAPI().bindAlias(alias, listener);
    }

    /**
     * 解除别名
     *
     * @param listener 回调
     * @return 执行结果
     */
    public boolean unbindAlias(PushListener listener) {
        if (getPushAPI() == null) {
            return false;
        }
        return getPushAPI().unbindAlias(listener);
    }

    /**
     * 查询标签
     *
     * @param listener 回调
     * @return 执行结果
     */
    public boolean queryTags(PushListener listener) {
        if (getPushAPI() == null) {
            return false;
        }
        return getPushAPI().queryTags(listener);
    }

    /**
     * 设置标签（重置）
     *
     * @param tags     标签数组，["tag1", "tag2"]
     * @param listener 回调
     * @return 执行结果
     */
    public boolean setTags(String[] tags, PushListener listener) {
        if (getPushAPI() == null) {
            return false;
        }
        return getPushAPI().setTags(tags, listener);
    }

    /**
     * 添加标签
     *
     * @param tags     标签数组，["tag1", "tag2"]
     * @param listener 回调
     * @return 执行结果
     */
    public boolean addTags(String[] tags, PushListener listener) {
        if (getPushAPI() == null) {
            return false;
        }
        return getPushAPI().addTags(tags, listener);
    }

    /**
     * 删除标签
     *
     * @param tags     标签数组，["tag1", "tag2"]
     * @param listener 回调
     * @return 执行结果
     */
    public boolean removeTags(String[] tags, PushListener listener) {
        if (getPushAPI() == null) {
            return false;
        }
        return getPushAPI().removeTags(tags, listener);
    }

    /**
     * 打开日志
     *
     * @param enable 打开或关闭
     */
    public void setLogEnable(boolean enable) {
        if (getPushAPI() == null) {
            return;
        }
        getPushAPI().setLogEnable(enable);
    }

    /**
     * 解析推送过来的数据
     *
     * @param context
     * @param payload 数据体，json格式
     */
    public void handlePush(Context context, final String payload) {
        if (TextUtils.isEmpty(payload)) {
            return;
        }
        PushMsgParser parser = PushMsgParser.newInstance(context, payload);
        parser.sendNotification();
    }

    public static class PushListener implements IPushListener {
        @Override
        public void onSuccess(int code, String message, String response) {
            Log.i(TAG, "PushListener onSuccess, code: " + code + ", message: " + message + "," +
                    " response: " + response);
        }

        @Override
        public void onFailure(int code, String message, String response) {
            Log.i(TAG, "PushListener onFailure, code: " + code + ", message: " + message + "," +
                    " response: " + response);
        }

    }

    public interface PushAPICallback {

        void onAPIReady(boolean ready);
    }

}
